import router from '@/router/router'

/**
 * 动态路由
 * @param parentList
 * @param permList
 */
export const parseRouterList = (menuList) => {
    if (menuList.length > 0){
        let route = getRoute(menuList,new Array());

        const Login = () => import('@/views/login/Login')
        const  Index = () => import('@/views/Index')

        router.addRoutes([
            {
                path: '/',
                redirect: '/index'
            },
            {
                path: '/login',
                name: "登录",
                component: Login
            },
            {
                path: '/index',
                name: "首页",
                component: Index,
                children: route
            }
        ]);
        return router
    }else {
        return null;
    }


}

/**
 * 递归生成路由
 * @param menu
 * @param route
 * @returns {*}
 */
function getRoute(parentMenu, route) {
    parentMenu.forEach((item, index) => {
        if (item.menuRole == '1') {
            //需要区分空路由和实际使用的路由。！！！！！
            if (item.menuLink != null && item.menuLink != ''){
                route.push({
                    // parent: item.parentId,
                    // icon:item.menuIcon,
                    path: item.menuLink,
                    name: item.menuName,
                    component: (resolve) => require([`@/views` + item.menuLink + item.menuLink+'List'], resolve)
                })
            }
        }

        if (item.children.length > 0){
            getRoute(item.children,route)
        }
    })
    return route;

}